
package fileby.gui.tabs;

import fileby.core.ActionKey;
import fileby.core.IDisplayableFileSource;
import fileby.ctrl.transferhandlers.ITransferHandlerProvider;
import fileby.core.TransferHandlerType;
import fileby.ctrl.actions.IActionProvider;
import fileby.util.properties.IPropertyBundleProvider;
import fileby.util.properties.PropertyBundle;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;

/**
 *
 * @author Administratör
 */
class TabComponent extends javax.swing.JPanel {

    private IDisplayableFileSource source;
    private ITransferHandlerProvider transferHandlerProvider;
    private IPropertyBundleProvider propertyBundleProvider;
    private IActionProvider actionProvider;

    public TabComponent(IDisplayableFileSource source,
            ITransferHandlerProvider transferHandlerProvider,
            IPropertyBundleProvider propertyBundleProvider,
            IActionProvider actionProvider) {

        this.transferHandlerProvider = transferHandlerProvider;
        this.source = source;
        this.propertyBundleProvider = propertyBundleProvider;
        this.actionProvider = actionProvider;
        initComponents();
        initCtrl();
    }

    public void setSource(IDisplayableFileSource source) {
        this.source = source;
        updateInfo();
    }

    private void initCtrl() {
        closeButton.setAction(actionProvider.getControl(ActionKey.CLOSE_TAB));
        setTransferHandler(transferHandlerProvider.create(TransferHandlerType.TAB_COMPONENT));
        closeButton.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseEntered(MouseEvent e){
                JButton me = (JButton)e.getSource();
                me.setForeground(Color.red);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                JButton me = (JButton)e.getSource();
                me.setForeground(Color.black);
            }
        });
    }

    private void updateInfo() {
        dirName.setText(source.getName());
        icon.setIcon(source.getIcon());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dirName = new javax.swing.JLabel();
        closeButton = new javax.swing.JButton();
        icon = new javax.swing.JLabel();

        setOpaque(false);

        PropertyBundle bundle = propertyBundleProvider.create(this.getClass());

        closeButton.setFont(bundle.getFont("closeButton.font")); // NOI18N
        closeButton.setContentAreaFilled(false);
        closeButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        updateInfo();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(icon, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dirName, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                .addComponent(closeButton)
                .addComponent(icon, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(dirName))
        );
    }// </editor-fold>
    
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel dirName;
    private javax.swing.JLabel icon;
    
}
